#include "confFile.h"

char* readthirdElement(char* line)
{
	char* third;
	strtok(line, " ");
	strtok(NULL, " ");
	third = strtok(NULL, " ");
	third = strtok(third, "\n");
	return third;
}

void readConfFile(char* fileName)
{
	FILE* f ;
	char line[50];
	f= fopen( fileName , "r");

	if (f != 0)
	{
		//read MaxNumOfProcesses
		fgets(line,50,f);
		MaxNumOfProcesses = atoi(readthirdElement(line));

		//read pageSize
		fgets(line,50,f);
		pageSize = atoi(readthirdElement(line));

		//read NumOfPagesInMM
		fgets(line,50,f);
		NumOfPagesInMM = atoi(readthirdElement(line));

		//read NumOfPagesInDisk
		fgets(line,50,f);
		NumOfPagesInDisk = atoi(readthirdElement(line));

		//read NumOfProcessPages
		fgets(line,50,f);
		NumOfProcessPages = atoi(readthirdElement(line));

		//read ShiftClock
		fgets(line,50,f);
		ShiftClock = atoi(readthirdElement(line));

		fclose(f);
	}
	sem_init(&clockSem, 0, ShiftClock);
	sem_init(&deamonSem, 0, 0);
	//sem_init(&tempsem, 0, 1);
	//pthread_mutex_init(&tempmutex,0);

	//calculate the real maxNumOfProcess
	if (NumOfProcessPages*MaxNumOfProcesses < NumOfPagesInDisk)
		NumOfPagesInDisk = NumOfProcessPages*MaxNumOfProcesses;
	else
		MaxNumOfProcesses = NumOfPagesInDisk / NumOfProcessPages;
		//else printf(err)
		/*printf("MaxNumOfProcesses: %d\n", MaxNumOfProcesses);
		printf("pageSize: %d\n", pageSize);
		printf("NumOfPagesInMM: %d\n", NumOfPagesInMM);
		printf("NumOfPagesInDisk: %d\n", NumOfPagesInDisk);
		printf("NumOfProcessPages: %d\n", NumOfProcessPages);
		printf("ShiftClock: %d\n", ShiftClock);*/
}
